package com.example.ah_geen_pda.login

import android.os.Bundle
import android.view.View
import com.example.ah_geen_pda.BR
import com.example.ah_geen_pda.MainActivity
import com.example.ah_geen_pda.R
import com.example.ah_geen_pda.base.BaseActivity
import com.example.ah_geen_pda.databinding.ActivityLoginBinding
import com.example.ah_geen_pda.entity.ToastMessageBean
import com.example.ah_geen_pda.setting.SettingActivity
import kotlinx.android.synthetic.main.activity_login.*
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions

class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>(), View.OnClickListener,EasyPermissions.PermissionCallbacks {
    override fun layoutId(): Int {
        return R.layout.activity_login
    }

    override fun initVariableId(): Int {
        return BR.loginViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        viewModel.initUser(this)
        viewModel.defUI.startActivity.observe(this){
            startActivity(MainActivity::class.java)
            finish()
        }
    }

    override fun initData() {
        btnLogin.setOnClickListener(this)
        ivSetting.setOnClickListener(this)
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>?) {

    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("用户授权失败", false)
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms!!)) {
            AppSettingsDialog.Builder(this).build().show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }


    override fun onClick(v: View?) {
        when(v?.id){
            R.id.btnLogin ->{
                viewModel.login(etUser.text.toString(),etPassword.text.toString())
            }
            R.id.ivSetting ->{
                startActivity(SettingActivity::class.java)
            }
        }
    }
}